package kr.co.happenstance.lib;

import java.net.URL;

import android.util.Log;
import kr.co.happenstance.C;
import kr.co.happenstance.http.HttpRequestor;
import kr.co.happenstance.lib.vo.LibResultVo;
import kr.co.happenstance.lib.vo.MemberCheckResultVo;
import kr.co.happenstance.lib.vo.MemberLastLocationResultVo;

public class MemberLib extends CommonLib {
	
	/**
	 * 회원 체크 API 메소드
	 * 
	 * @return
	 * @throws Exception
	 */
	public LibResultVo memberCheck(String id) throws Exception{
		MemberCheckResultVo resultVo = null;
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_MEMBER_CHECK));
			httpRequestor.addParameter("id", id);
			
			resultVo = super.send(MemberCheckResultVo.class);
			Log.d(TAG, "["+resultVo.getResult()+"]");
			Log.d(TAG, "["+resultVo.getMessage()+"]");
			Log.d(TAG, "["+resultVo.getMemberGbn()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		return resultVo;
	}
	
	/**
	 * 로그인 API 메소드
	 * 
	 * @param id
	 * @param gender
	 * @param birthday
	 * @param email
	 * @param latitude
	 * @param longitude
	 * @return
	 * @throws Exception
	 */
	public LibResultVo login(String id, String gender, String birthday, String email, String latitude, String longitude, String gcmRegisterId) throws Exception{
		LibResultVo resultVo = null;
		
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_LOGIN));
			httpRequestor.addParameter("id", id);
			httpRequestor.addParameter("gender", gender);
			httpRequestor.addParameter("birthday", birthday);
			httpRequestor.addParameter("email", email);
			httpRequestor.addParameter("latitude", latitude);
			httpRequestor.addParameter("longitude", longitude);
			httpRequestor.addParameter("gcmRegisterId", gcmRegisterId);
			
			resultVo = super.send(LibResultVo.class);
			Log.d(TAG, "["+resultVo.getResult()+"]");
			Log.d(TAG, "["+resultVo.getMessage()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		
		return resultVo;
	}
	
	/**
	 * 회원가입 API 메소드
	 * 
	 * @param id
	 * @param gender
	 * @param birthday
	 * @param email
	 * @return
	 * @throws Exception
	 */
	public LibResultVo join(String id, String gender, String birthday, String email) throws Exception{
		LibResultVo resultVo = null;
		
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_MEMBER_INSERT));
			httpRequestor.addParameter("id", id);
			httpRequestor.addParameter("gender", gender);
			httpRequestor.addParameter("birthday", birthday);
			httpRequestor.addParameter("email", email);
			
			resultVo = super.send(LibResultVo.class);
			Log.d(TAG, "["+resultVo.getResult()+"]");
			Log.d(TAG, "["+resultVo.getMessage()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		
		return resultVo;		
	}
	
	/**
	 * GCM Registeration ID 등록 API 메소드 
	 * 
	 * @param id
	 * @param gcmRegisterId
	 * @return
	 * @throws Exception
	 */
	public LibResultVo memberGcmRegisterIdUpdate(String id, String gcmRegisterId) throws Exception{
		LibResultVo resultVo = null;
		
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_MEMBER_GCM_ID_UPDATE));
			httpRequestor.addParameter("id", id);
			httpRequestor.addParameter("gcmRegisterId", gcmRegisterId);
			
			resultVo = super.send(LibResultVo.class);
			Log.d(TAG, "["+resultVo.getResult()+"]");
			Log.d(TAG, "["+resultVo.getMessage()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		return resultVo;				
	}
	
	
	/**
	 * 회원 위치 정보 조회 API 메소드
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public LibResultVo memberLastLocation(String id) throws Exception{
		MemberLastLocationResultVo resultVo = null;
		
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_MEMBER_LAST_LOCATION));
			httpRequestor.addParameter("id", id);
			
			resultVo = super.send(MemberLastLocationResultVo.class);
			Log.d(TAG, "result["+resultVo.getResult()+"]");
			Log.d(TAG, "message["+resultVo.getMessage()+"]");			
			Log.d(TAG, "latitude["+resultVo.getLatitude()+"]");
			Log.d(TAG, "longitude["+resultVo.getLongitude()+"]");
			
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		
		return resultVo;
	}
	
	/**
	 * 회원 위치 정보 갱신
	 * 
	 * @param id
	 * @param latitude
	 * @param longitude
	 * @return
	 * @throws Exception
	 */
	public LibResultVo memberLocationUpdate(String id, String latitude, String longitude) throws Exception{
		LibResultVo resultVo = null;
		
		try{
			httpRequestor =  new HttpRequestor(new URL(C.API_URL_MEMBER_LOCATION_UPDATE));
			httpRequestor.addParameter("id", id);
			httpRequestor.addParameter("latitude", latitude);
			httpRequestor.addParameter("longitude", longitude);
			
			resultVo = super.send(LibResultVo.class);
			Log.d(TAG, "["+resultVo.getResult()+"]");
			Log.d(TAG, "["+resultVo.getMessage()+"]");
			
		}catch(Exception e){
			e.printStackTrace();
			resultVo = null;
		}
		
		return resultVo;
	}
	
}
